package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.CreateStateException;
import com.management.commons.exception.DeleteStateException;
import com.management.commons.exception.DisableByStateIdException;
import com.management.commons.exception.ExceptionCode.DefectStateExceptionCode;
import com.management.commons.exception.GetStateException;
import com.management.controller.form.*;
import com.management.controller.info.GetStateInfo;
import com.management.dao.DefectRecordDao;
import com.management.dao.ProjectInfoDao;
import com.management.model.DefectState;
import com.management.dao.DefectStateDao;
import com.management.model.ProjectInfo;
import com.management.service.DefectStateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class DefectStateServiceImpl extends ServiceImpl<DefectStateDao, DefectState> implements DefectStateService {

    @Resource
    private DefectRecordDao defectRecordDao;
    @Override
    public Result FindStates(GetStatePageForm getStatePageForm) {
        Page<DefectState> Page = new Page<>();
        GetStatePageForm pageForm=null;
        if (null== getStatePageForm.getStateDesc()) {
            pageForm = baseMapper.selectPage(getStatePageForm, null);
        }else {
            pageForm = baseMapper.selectPage(getStatePageForm, new QueryWrapper<DefectState>().like("state_desc", getStatePageForm.getStateDesc()));
        }
        if (null!=pageForm) {
            List<DefectState> records = pageForm.getRecords();
            List list = CopyUtils.CopyList(records, GetStateInfo.class);

            Page.setTotal(pageForm.getTotal());
            Page.setCurrent(pageForm.getCurrent());
            Page.setSize(pageForm.getSize());
            Page.setPages(pageForm.getPages());
            Page.setRecords(list);
            return Result.getInstance().setData(Page);
        }
        else throw new GetStateException(DefectStateExceptionCode.GETSTATE_NULL_ERROR);
    }

    @Override
    public Result CreateState(CreateStateForm createStateForm) {
        QueryWrapper<DefectState> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state_desc",createStateForm.getStateDesc());
        DefectState defectState1 = baseMapper.selectOne(queryWrapper);
        if (defectState1==null) {
            DefectState defectState = CopyUtils.CopyOne(createStateForm, DefectState.class);
            int i = baseMapper.insert(defectState);
            if (i>0) {
                return Result.getInstance().setMessage("新增成功");
            }
            else throw new CreateStateException(DefectStateExceptionCode.CREATESTATE_ERROR);
        }
        throw new CreateStateException(DefectStateExceptionCode.STATEDESCISEXISTS_ERROR);

    }

    @Override
    public Result removState(RemoveStateForm removeStateForm) {
        int i = baseMapper.deleteById(removeStateForm.getStateId());
        if (i>0) {
            return Result.getInstance().setMessage("删除成功");
        }
        else throw new DeleteStateException(DefectStateExceptionCode.DELETESTATE_ERROR);
    }

    @Override
    public Result removeBystateIds(RemoveBysateIdsForm removeBysateIdsForm) {
        int i=0;
        for (Integer integer : removeBysateIdsForm.getList()) {
            i += baseMapper.deleteById(integer);
        }
        if (i>0) {
            return Result.getInstance().setMessage("删除成功");
        }
        else throw new DeleteStateException(DefectStateExceptionCode.DELETESTATE_ERROR);
    }

    @Override
    public Result updateBystateId(DisableStateForm disableStateForm) {
        DefectState defectState = baseMapper.selectById(disableStateForm.getStateId());
        if (defectState.getModuleState().equals("启用")) {
            defectState.setModuleState("禁用");
        }else {
            defectState.setModuleState("启用");
        }
        int i = baseMapper.updateById(defectState);
        if (i>0) {
            return Result.getInstance().setMessage("修改成功");
        }
        else throw new DisableByStateIdException(DefectStateExceptionCode.DISABLEBYSTATEID_ERROR);
    }

    @Override
    public Result FindAllDefectState() {
        List<DefectState> defectStates = baseMapper.selectList(null);
        List list = CopyUtils.CopyList(defectStates, GetStateInfo.class);
        return Result.getInstance().setData(list);
    }

    @Override
    public Result getAllDefect() {
       List<AllStateCountForm> list=defectRecordDao.findAllStateCount();

        return Result.getInstanceList(AllStateCountForm.class).setData(list);
    }


}
